package com.youxin.busfoundation.common.es;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.youxin.busfoundation.common.es.bean.CompanyIndexBean;
import com.youxin.foundation.utils.JsonUtils;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;

import java.util.Arrays;
import java.util.Map;

/**
 * @desc ES操作基础类
 * @author Kris.wang
 * @date 2018-12-18i
 */
public class BaseHandle<K,V>{

    /**
     * @desc 构造函数
     */
    public  BaseHandle() {}

    /**
     * @desc 创建索引文档
     * @param index 索引
     * @param type 类型
     * @param id 文档ID
     * @param source 文档Map数据
     * @return
     * @throws Exception
     */
    public boolean createIndex( String index, String type, String id, Map<String,Object> source) throws Exception
    {
        Client client = EsClient.instance().getClient();
        try {
            IndexResponse response = client.prepareIndex(index,type)
                    .setId(id)
                    .setSource(source)
                    .execute()
                    .actionGet();
            if (response.getResult().toString() == "CREATED"
                    || response.getResult().toString() =="UPDATED") {
                return true;
            }
        } catch (Exception e) {
            throw e;
        }

        return false;
    }

    /**
     * @desc 获取索引文档
     * @param index
     * @param type
     * @param id
     * @return
     * @throws Exception
     */
    public String getIndex(String index, String type, String id) throws Exception
    {
        Client client = EsClient.instance().getClient();
        try {
            GetResponse response = client
                    .prepareGet(index,type,id)
                    .setOperationThreaded(false)
                    .get();
            return response.getSourceAsString();
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * @desc 更新索引文档
     * @param index
     * @param type
     * @param id
     * @param doc
     * @return
     * @throws Exception
     */
    public boolean updateIndex(String index, String type, String id, Map<String,Object> doc) throws Exception
    {
        Client client = EsClient.instance().getClient();
        try {
            UpdateRequest updateRequest = new UpdateRequest();
            updateRequest.index(index);
            updateRequest.type(type);
            updateRequest.id(id);
            updateRequest.doc(doc);
            UpdateResponse response = client
                    .update(updateRequest)
                    .get();
            if (response.getResult().toString() == "CREATED"
                    || response.getResult().toString() =="UPDATED") {
                return true;
            }
        } catch (Exception e) {
            throw e;
        }

        return true;
    }

    /**
     * @desc 删除索引文档
     * @param index
     * @param type
     * @param id
     * @return
     * @throws Exception
     */
    public boolean deleteIndex(String index, String type, String id) throws Exception
    {
        Client client = EsClient.instance().getClient();
        try {
            DeleteResponse response = client
                    .prepareDelete(index,type,id)
                    .get();
            if (response.getResult().toString() == "DELETED") {
                return true;
            }
        } catch (Exception e) {
            throw e;
        }

        return false;
    }

    /**
     * @desc Bean对象Map
     * @param object
     * @return
     * @throws Exception
     */
    public Map<String, Object>  objectToMap(Object object) throws Exception
    {
        String json = new ObjectMapper().writeValueAsString(object);
        return JsonUtils.objectToMapObject(json);
    }

}
